package com.example.order_system.controller;

import com.example.order_system.util.ConstVariable;
import com.example.order_system.util.ResultData;
import com.example.order_system.model.UserInfo;
import com.example.order_system.service.UserService;
import com.example.order_system.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @description: UserController
 * @author: chengbaiyi
 * @create: 2022-07-22 14:18
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 注册方法
     *
     * @param username 用户名
     * @param password 密码
     * @return ResultData
     */
    @PostMapping("/reg")
    public ResultData reg(String username, String password) {
        int data = -1;
        int state = -1;
        String msg;
        if (StringUtils.hasLength(username) && StringUtils.hasLength(password)) {
            // 判断用户名是否存在
            if (userService.existed(username) >= 1) {
                msg = "用户名已存在";
            } else {
                password = PasswordUtil.encrypt(password);
                data = userService.reg(username, password);
                if (data == 1) {
                    // 注册成功
                    state = 200;
                    msg = "注册成功";
                } else {
                    msg = "注册失败";
                }
            }
        } else {
            msg = "参数错误";
        }
        return new ResultData(state, data, msg);
    }

    /**
     * 登录
     *
     * @param username 用户名
     * @param password 密码
     * @param isadmin  是否是管理员
     * @return ResultData
     */
    @PostMapping("/login")
    public ResultData login(HttpServletRequest req, String username, String password, String isadmin) {
        int state = -1;
        String msg = "未知错误";
        String data = "";
        // 验证参数
        if (StringUtils.hasLength(username) && StringUtils.hasLength(password)) {
            UserInfo userInfo;
            // 判断是否是管理员登录
            if (StringUtils.hasLength(isadmin) && isadmin.equals("1")) {
                // 管理员登录
                userInfo = userService.login(username, isadmin);
            } else {
                // 普通用户登录
                userInfo = userService.login(username, null);
            }
            if (userInfo != null) {
                // 判断密码是否正确
                boolean result = PasswordUtil.decrypt(password, userInfo.getPassword());
                if (result) {
                    state = 200;
                    msg = "登录成功";
                    data = username;
                    // 将用户名和密码保存到 session 中
                    HttpSession session = req.getSession(true);
                    session.setAttribute(ConstVariable.USER_SESSION_KEY, userInfo);
                } else {
                    msg = "用户名或密码错误";
                }
            }
        } else {
            msg = "未知错误";
        }
        return new ResultData(state, data, msg);
    }

    /**
     * 获得 session
     *
     * @param req req
     * @return ResultData
     */
    @GetMapping("/sess")
    public ResultData getSession(HttpServletRequest req) {
        int state = -1;
        String msg = "";
        String data = "";
        UserInfo userInfo = null;
        HttpSession session = req.getSession(false);
        if (session != null && session.getAttribute(ConstVariable.USER_SESSION_KEY) != null) {
            userInfo = (UserInfo) session.getAttribute(ConstVariable.USER_SESSION_KEY);
        }
        if (userInfo != null) {
            // 已登录
            state = 200;
            data = userInfo.getUsername();
            msg = "已登录";
        } else {
            msg = "未登录";
        }
        return new ResultData(state, data, msg);
    }

    /**
     * 退出登录
     *
     * @param req request
     * @return ResultData
     */
    @PostMapping("/logout")
    public ResultData logout(HttpServletRequest req) {
        int state = -1;
        HttpSession session = req.getSession(false);
        if (session != null && session.getAttribute(ConstVariable.USER_SESSION_KEY) != null) {
            session.removeAttribute(ConstVariable.USER_SESSION_KEY);
            state = 200;
        }
        return new ResultData(state, null, null);
    }
}
